package com.et.machine.store

import android.database.Cursor
import androidx.room.*
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Single

interface BaseDao

@Dao
interface IOutGoodsFlagDao : BaseDao {

    @Query("SELECT * FROM table_out_goods_order ORDER BY id DESC LIMIT :limit")
    fun query(limit: Int): Flowable<MutableList<OutGoodsRecordEntity>>

    @Insert(onConflict = OnConflictStrategy.IGNORE, entity = OutGoodsRecordEntity::class)
    fun insert(trade: OutGoodsRecordEntity): Completable

    @Query("DELETE FROM table_out_goods_order WHERE id NOT IN (SELECT id FROM table_out_goods_order ORDER BY id DESC LIMIT:maxKeepCount)")
    fun deleteOver(maxKeepCount: Int = 1000): Int

}


@Dao
interface IChannelDao : BaseDao {

    @Query("SELECT * FROM table_channel")
    fun all(): Flowable<MutableList<ChannelEntity>>

    @Query("SELECT * FROM table_channel")
    fun findAll(): Cursor

    @Query("SELECT * FROM table_channel WHERE box IN (:box) AND channel IN (:channel) limit 1")
    fun single(box: Int, channel: Int): Flowable<MutableList<ChannelEntity>>

    @Insert(onConflict = OnConflictStrategy.REPLACE, entity = ChannelEntity::class)
    fun insert(channel: ChannelEntity): Completable

    @Insert(onConflict = OnConflictStrategy.REPLACE, entity = ChannelEntity::class)
    fun inserts(channel: MutableList<ChannelEntity>): Completable

    @Update(entity = ChannelEntity::class)
    fun update(channel: ChannelEntity): Completable

    @Update(entity = ChannelEntity::class)
    fun updateByProvider(channel: ChannelEntity): Int

    @Delete(entity = ChannelEntity::class)
    fun delete(channel: ChannelEntity): Single<Int>

    @Query("DELETE FROM table_channel WHERE id in (:ids)")
    fun delete(ids: String): Int

    @Query("DELETE FROM table_channel")
    fun delete(): Completable
}
